﻿//--------------------------------------------------------------------------------------------------------------------------------------------------------
// <copyright file="UserActivationSteps.cs">
//     Copyright © 2010 Oli Francis
//     This source code is subject to the terms and conditions of the MIT license. A copy of the license can be found in the license.txt 
//     file at the root of this distribution. By using this source code in any fashion, you are agreeing to be bound by the terms of the MIT License. 
//     You must not remove this notice from the software.
// </copyright>
// <author>Oli</author>
//-------------------------------------------------------------------------------------------------------------------------------------------------------

namespace TeamBrain.Tests.StepDefinitions
{
    #region Using Statements

    using System.Web.Mvc;
    using Moq;
    using NUnit.Framework;

    using TeamBrain.Domain.Model;
    using TeamBrain.Domain.Services.Exceptions;
    using TeamBrain.Domain.Services.Registration;
    using TeamBrain.Tests.Utilities;
    using TeamBrain.Web.Controllers;
    using TeamBrain.Web.Utilities;

    using TechTalk.SpecFlow;

    #endregion

    /// <summary>
    /// Defines methods that bind to the scenario steps for the Registration feature
    /// </summary>
    [Binding]
    [StepScope(Feature = "User Activation")]
    public class UserActivationSteps
    {
        private Mock<IRegistrationService> registrationMock;
        private RegistrationController controller;
        private ActionResult result;

        [BeforeScenario]
        public void Initialize()
        {
            this.registrationMock = new Mock<IRegistrationService>();
            this.controller = new RegistrationController(this.registrationMock.Object);
        }

        [Given(@"I have received a verification email")]
        public void GivenIHaveReceivedAVerificationEmail()
        {
        }
        
        [Given(@"I copy the activation link into a browser")]
        public void GivenICopyTheActivationLinkIntoABrowser()
        {
        }

        [Given(@"I make a mistake and don't copy all of the characters")]
        public void GivenIMakeAMistakeAndDonTCopyAllOfTheCharacters()
        {
            this.registrationMock
                .Setup(r => r.ActivateUserFromToken(It.IsAny<string>()))
                .Throws(new InvalidActivationTokenException());
        }

        [When(@"I browse to the url")]
        public void WhenIBrowseToTheUrl()
        {
            this.result = this.controller.Activate("tokenvalue");
        }
        
        [Then(@"I see an error page explaining that activation failed")]
        public void ThenISeeAnErrorPageExplainingThatActivationFailed()
        {
            var redirect = this.result as RedirectToRouteResult;
            Assert.IsNotNull(redirect);
            Assert.AreEqual("ActivationFailed", redirect.RouteValues["action"]);
        }

        [Then(@"I see a success confirmation page")]
        public void ThenISeeASuccessConfirmationPage()
        {
            var redirect = this.result as RedirectToRouteResult;
            Assert.IsNotNull(redirect);
            Assert.AreEqual("AccountActivated", redirect.RouteValues["action"]);
        }
    }
}
